<template>
  <ex-dialog width="50%" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="145px" class="demo-ruleForm">
        
        <el-form-item label="虚拟用户手机号：" prop="customerPhone">
          <el-input v-model="ruleForm.customerPhone" placeholder="请输入虚拟用户手机号：" style="width:280px"></el-input>
        </el-form-item>
        <el-form-item label="虚拟用户头像：" prop="customerAvatar">
          <el-upload class="avatar-uploader2" :action="action" :show-file-list="false" :on-success="imgSuccess"
            :on-error="onError" :on-progress="onProgress" :multiple="false">
            <img class="banner_img" :src="ruleForm.customerAvatar" alt="" v-if="ruleForm.customerAvatar">
            <i class="el-icon-plus" style="font-size: 28px" v-else v-loading="imgLoading"></i>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      
      <el-button type="primary"  @click="submitForm('ruleForm')" v-if="id!=null">保存</el-button>
      <el-button type="primary"  @click="handleCancel('ruleForm')" v-if="id==null">取消</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  // components
  import Loading from "@/components/Loading/index";
  // api
  import {
    getBannerInfo,
    addVirtual,
  } from "@/api/blindBox/activityDetail";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
    },
    data() {
      return {
        title:'添加虚拟用户',
        loading: false,
        action: process.env.VUE_APP_BASE_API + "/common/v1/upload",
        imgLoading: false,
        ruleForm: {
          activityId: '',//盲盒活动id
          customerAvatar: '', //用户头像
          customerPhone:"",//配置名称 
        },
        rules: {
          customerPhone: [{
            required: true,
            message: "请输入名称",
            trigger: ["blur","change"]
          }],
          customerAvatar: [{
            required: true,
            message: "请上传图片",
            trigger: "change"
          }],
        },
      };
    },
    computed: {},
    created() {
      
      this.ruleForm.activityId = this.id
    },
    methods: {
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
    
      imgSuccess(res, file) { //文件上传成功时
        console.log(res.result.url)
        this.ruleForm.customerAvatar = res.result.url
        this.$refs.ruleForm.validateField('customerAvatar')
        this.imgLoading = false
      },
      onProgress(file) { //文件上传时
        this.imgLoading = true
      },
      onError(file) {
        this.imgLoading = false
      },
      getBannerInfo() { //获取商家团购信息详情
        this.loading = true
        const params = {
          id: this.id
        };
        getBannerInfo(params).then((res) => {
          let info = res.result
          this.ruleForm.name = info.name
          this.ruleForm.type = info.type
         
        });
      },
      handleCancel(){
        this.handleClose()
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //   alert("submit!");editGroupon
            var params = {
              ...this.ruleForm,
            };
            addVirtual(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {

              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      editForm(formName) {
        //编辑确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //   alert("submit!");editGroupon
            var params = {
              id: this.id,
              ...this.ruleForm,
            };
            editBanner(params).then((res) => {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {
                
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .from_box {
    display: flex;
    margin-bottom: 20px;
  }

  .from_box_l {
    width: 110px;
    color: #606266;
    font-size: 14px;
    font-weight: bold;
  }

  .from_box_r {
    color: #333;
    font-size: 15px;
    flex: 1;
  }

  .el_table_box {
    padding: 0 20px 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .el_table_box_h {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f3f3f3;
    margin-bottom: 14px;
  }

  .el_table_box_h_title {
    border-left: 4px solid #ff9300;
    padding-left: 4px;
  }

  .carousel_box {
    width: 400px;
    margin-bottom: 20px;
  }

  .swiper_img {
    width: 100%;
    height: 100%;
  }

  .carousel_box_two {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }

  .phone_box {
    width: 100px;
    height: 100px;
    margin: 0 10px 10px 0;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  ::v-deep .avatar-uploader2 .el-upload {
    width: 100% !important;
  }

  .avatar-uploader2 {
    width: 228px;
    height: 128px;
    text-align: center;
    line-height: 128px;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
  }

  .banner_img {
    width: 228px;
    height: 128px;
  }

</style>
